package cc.mrbird.web.controller;

import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.common.util.FileUtils;
import cc.mrbird.web.domain.RespImport;
import cc.mrbird.web.domain.StudentTemp;
import cc.mrbird.web.service.RiseStudentService;
import cc.mrbird.web.service.StudentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;


@Controller
public class RiseExcelImportController {

    private static final Logger log = LoggerFactory.getLogger(RiseExcelImportController.class);

    @Autowired
    private RiseStudentService riseStudentServices;

    @Log("导入考生信息")
    @RequestMapping("rise_import")
    @RequiresPermissions("rise_import:add")
    public String imports() {
        return "web/rise_import/rise_import";
    }


    @PostMapping("/riseStudentImport")
    @ResponseBody
    public RespImport studentImport(@RequestParam("file") MultipartFile file, String userName) {
        log.info("startTime:" + System.currentTimeMillis());
        RespImport respImport = riseStudentServices.ImportRiseStudent(file, userName);
        log.info("endTime:" + System.currentTimeMillis());
        return respImport;
    }

    @RequestMapping("/downloadTemplate")
    @ResponseBody
    public ResponseBo templateExcel() {
        StudentTemp student1 = new StudentTemp();
        student1.setStudentId("17410101110337（必须为14位）");
        student1.setName("张三（必填）");
        student1.setGraduateAdd("郑州成功财经学院（必填）");
        student1.setNote("备注，允许为空");

        StudentTemp student = new StudentTemp();
        student.setStudentId("17410101112117（必须为14位）");
        student.setName("李四（必填）");
        student.setGraduateAdd("郑州成功财经学院（必填）");
        student.setNote("填写备注，允许为空");

        ArrayList<StudentTemp> list = new ArrayList<>();
        list.add(student);
        list.add(student1);
        try {
            return FileUtils.createExcelByPOIKit("专科考生信息模板", list, StudentTemp.class);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBo.error("导出Excel失败，请联系网站管理员！");
        }
    }
}